package ca.uwo.android.campusmap;

import android.app.SearchManager;
import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;
import android.provider.BaseColumns;

public class BuildingContentProvider extends ContentProvider {

	public static String AUTHORITY = "ca.uwo.android.campusmap.BuildingContentProvider";
	public static final Uri CONTEN_URI = Uri.parse("content://" + AUTHORITY + "/building");

	private static final int SEARCH_BUILDING = 0;
	private static final int SEARCH_SUGGESTION = 1;
	private static final int GET_BUILDING = 2;

	private static final UriMatcher uriMatch = getUriMatcher();

	private BuildingDatabase mBuildings;

	@Override
	public String getType(Uri uri) {
		switch (uriMatch.match(uri)) {
		case SEARCH_BUILDING:
			return ContentResolver.CURSOR_DIR_BASE_TYPE + "/vnd.ca.uwo.android.campusmap";
		case SEARCH_SUGGESTION:
			return SearchManager.SUGGEST_MIME_TYPE;
		case GET_BUILDING:
			return ContentResolver.CURSOR_ITEM_BASE_TYPE + "/vnd.ca.uwo.android.campusmap";
		default:
			throw new IllegalArgumentException("Unknown URL " + uri);
		}
	}

	private static UriMatcher getUriMatcher() {
		UriMatcher match =  new UriMatcher(UriMatcher.NO_MATCH);
		match.addURI(AUTHORITY, "building", SEARCH_BUILDING);
		match.addURI(AUTHORITY, "building/#", GET_BUILDING);

		match.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY, SEARCH_SUGGESTION);
		match.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY + "/*", SEARCH_SUGGESTION);

		return match;
	}

	@Override
	public boolean onCreate() {
		mBuildings = new BuildingDatabase(getContext());
		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {

		switch(uriMatch.match(uri)) {
		case SEARCH_BUILDING:
			if (selectionArgs == null) {
				throw new IllegalArgumentException("missing arguments: " + uri);
			}
			return getSearch(selectionArgs[0]);
		case SEARCH_SUGGESTION:
			if (selectionArgs == null) {
				throw new IllegalArgumentException("missing arguments: " + uri);
			}
			return getSuggestions(selectionArgs[0]);
		case GET_BUILDING:
			return getBuildling(uri);
		default:
			throw new IllegalArgumentException("Not supported: " + uri);
		}
	}

	private Cursor getBuildling(Uri uri) {
		String rowId = uri.getLastPathSegment();
		String[] columns = new String[] {
				BaseColumns._ID,
				BuildingDatabase.BUILDING_NAME,
				BuildingDatabase.LONGITUDE,
				BuildingDatabase.LATITUDE,
				BuildingDatabase.ADDRESS };
		return mBuildings.getBuilding(rowId, columns);
	}

	private Cursor getSuggestions(String query) {
		query = query.toLowerCase();
		String[] columns = new String[] {
				BaseColumns._ID,
				BuildingDatabase.BUILDING_NAME,
				SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID
				};

		Cursor result = mBuildings.getWordMatches(query, columns);
		return result;
	}

	private Cursor getSearch(String query) {
		query = query.toLowerCase();
		String[] columns = new String[] {
				BaseColumns._ID,
				BuildingDatabase.BUILDING_NAME, 
				BuildingDatabase.LONGITUDE,
				BuildingDatabase.LATITUDE,
				BuildingDatabase.ADDRESS };

		return mBuildings.getWordMatches(query, columns);
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		throw new UnsupportedOperationException();
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		throw new UnsupportedOperationException();
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		throw new UnsupportedOperationException();
	}
}
